"""
https://www.tensorflow.org/guide/keras/train_and_evaluate
"""

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, losses, metrics, optimizers, Model, models
from python_ai.common.xcommon import *
import os
import sys
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# data
x, y = load_breast_cancer(return_X_y=True)
x = StandardScaler().fit_transform(x)

x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=777)
m, n = x_train.shape
sep((m, n))

# Reserve 10,000 samples for validation
n_val = int(np.ceil(m * 0.1))
x_val = x_train[-n_val:]
y_val = y_train[-n_val:]
x_train = x_train[:-n_val]
y_train = y_train[:-n_val]

# model
L1 = 128
L2 = 64
# inputs = keras.Input(shape=(n,), name="digits")
# x = layers.Dense(L1, activation="relu", name="dense_1")(inputs)
# x = layers.Dense(L2, activation="relu", name="dense_2")(x)
# outputs = layers.Dense(1, activation="sigmoid", name="predictions")(x)
# model = Model(inputs=inputs, outputs=outputs)

model = models.Sequential([
    layers.Dense(L1, input_dim=n, activation=tf.nn.relu),
    layers.Dense(L2, activation=tf.nn.relu),
    layers.Dense(1, activation=tf.nn.sigmoid)
])

# specify the training config
model.compile(
    optimizer=optimizers.RMSprop(),
    loss=losses.binary_crossentropy,
    metrics=[metrics.binary_accuracy]
)

# fit and train
sep('Fit and train')
history = model.fit(
    x_train,
    y_train,
    batch_size=64,
    epochs=2,
    validation_data=(x_val, y_val)
)
print(history.history)

sep('Evaluate on test data')
results = model.evaluate(x_test, y_test, batch_size=128)
print(f'test loss, test acc: {results}')

